﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using System.Runtime.InteropServices;

namespace RecruitStudents.Client.UserControls
{
    public partial class UserControl_ReadIdCard : UserControl
    {
        #region 读卡器
        public int iRetUSB = 0;
        public int iRetCOM = 0;
        public StringBuilder name;      //姓名
        public String sex;              //性别
        public String people;           //民族，护照识别时此项为空
        public String birthday;         //出生日期
        public String address;          //地址，在识别护照时导出的是国籍简码
        public string number;           //地址，在识别护照时导出的是国籍简码
        public string signdate;         //签发日期，在识别护照时导出的是有效期至 
        public string validtermOfStart; //有效起始日期，在识别护照时为空
        public string validtermOfEnd;   //有效截止日期，在识别护照时为空

        //扫描结构：
        [StructLayout(LayoutKind.Sequential, Size = 16, CharSet = CharSet.Ansi)]
        public struct IDCARD_ALL
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
            public char name;               //姓名
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]
            public char sex;                //性别
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
            public char people;             //民族，护照识别时此项为空
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public char birthday;           //出生日期
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 70)]
            public char address;            //地址，在识别护照时导出的是国籍简码
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
            public char number;             //地址，在识别护照时导出的是国籍简码
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
            public char signdate;           //签发日期，在识别护照时导出的是有效期至 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public char validtermOfStart;   //有效起始日期，在识别护照时为空
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public char validtermOfEnd;     //有效截止日期，在识别护照时为空
        }

        private void InitCardData()
        {
            try
            {
                int iPort;
                for (iPort = 1001; iPort <= 1016; iPort++)
                {
                    iRetUSB = CheckIDCard.CVR_InitComm(iPort);
                    if (iRetUSB == 1)
                        break;
                }
                for (iPort = 1; iPort <= 4; iPort++)
                {
                    iRetCOM = CheckIDCard.CVR_InitComm(iPort);
                    if (iRetCOM == 1)
                        break;
                }
            }
            catch
            {
            }
        }
        #endregion 

        #region 身份证读卡器
        private void FillData()
        {
            try
            {
                byte[] name = new byte[30];
                int length = 30;
                CheckIDCard.GetPeopleName(ref name[0], ref length);
                //MessageBox.Show();
                byte[] number = new byte[30];
                length = 36;
                CheckIDCard.GetPeopleIDCode(ref number[0], ref length);
                byte[] people = new byte[30];
                length = 3;
                CheckIDCard.GetPeopleNation(ref people[0], ref length);
                byte[] validtermOfStart = new byte[30];
                length = 16;
                CheckIDCard.GetStartDate(ref validtermOfStart[0], ref length);
                byte[] birthday = new byte[30];
                length = 16;
                CheckIDCard.GetPeopleBirthday(ref birthday[0], ref length);
                byte[] address = new byte[30];
                length = 70;
                CheckIDCard.GetPeopleAddress(ref address[0], ref length);
                byte[] validtermOfEnd = new byte[30];
                length = 16;
                CheckIDCard.GetEndDate(ref validtermOfEnd[0], ref length);
                byte[] signdate = new byte[30];
                length = 30;
                CheckIDCard.GetDepartment(ref signdate[0], ref length);
                byte[] sex = new byte[30];
                length = 3;
                CheckIDCard.GetPeopleSex(ref sex[0], ref length);
                txtIdCard.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(number).Replace("\0", "");
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("读取身份证出现异常：" + ex.ToString());
            }
        }
        #endregion

        internal void TextSelectAll()
        {
            txtIdCard.SelectAll();
        }
        public void TextFocus()
        {
            txtIdCard.Focus();
        }
        public bool TextEnabled
        {
            get
            {
                return txtIdCard.Enabled;
            }
            set
            {
                txtIdCard.Enabled = value;
            }
        }
        public string TextValue
        {
            get
            {
                return txtIdCard.Text;
            }
            set
            {
                txtIdCard.Text = value;
            }
        }
        public UserControl_ReadIdCard()
        {
            InitializeComponent();
            InitCardData();
        }
        public delegate void Button_ClickEventHandler(object sender, EventArgs e);
        public event Button_ClickEventHandler Button_Click_Event;

        public bool ReadCard()
        {
            try
            {
                if (System.IO.File.Exists(Application.StartupPath + "\\zp.bmp"))
                {
                    System.IO.File.Delete(Application.StartupPath + "\\zp.bmp");
                }
                if ((iRetCOM == 1) || (iRetUSB == 1))
                {
                    int authenticate = CheckIDCard.CVR_Authenticate();
                    if (authenticate == 1)
                    {
                        int readContent = CheckIDCard.CVR_Read_Content(4);
                        if (readContent == 1)
                        {
                            FillData();
                            return true;
                        }
                    }
                }
                else
                {
                    DevHelper.ShowErrorMessageBox("初始化失败！");
                }
                return false;
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox(ex.ToString());
                return false;
            }
        }

        public delegate void Text_KeyPressEventHandler(object sender, KeyPressEventArgs e);
        public event Text_KeyPressEventHandler Text_KeyPress;
        protected void OnText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Text_KeyPress != null)
            {
                Text_KeyPress(sender, e);
            }
        }
        private void txtIdCard_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnText_KeyPress(this, e);
        }
    }
}
